﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Heightmap_Editor.Menu.Screens
{
    class ImportScreen : MenuScreen
    {
        private MenuProgressBar progress;
        private MenuWidget message;

        public ImportScreen()
            : base("Open Maps")
        {
            MenuEntry open = new MenuEntry("Open Heightmap...");
            progress = new MenuProgressBar(0);
            message = new MenuWidget("Image size must be\na multiple of 64!");

            open.Selected += open_Selected;

            MenuEntries.Add(open);
            MenuWidgets.Add(progress);
        }

        void open_Selected(object sender, GameStateManagement.PlayerIndexEventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog() { Title = "Open Heightmap", Filter = "Jpeg Images|*.jpg|Png Images|*.png" };

            if (opf.ShowDialog() == DialogResult.OK)
            {
                Thread import = new Thread(ImportFile);
                import.Start(opf.FileName);
            }
        }

        void ImportFile(object filename)
        {
            var file = new FileStream((string)filename, FileMode.Open, FileAccess.ReadWrite);
            Texture2D texture = Texture2D.FromStream(HeightmapEditor.Instance.GraphicsDevice, file);
            file.Close();

            if ((int)(texture.Width / 64) * 64 != texture.Width && (int)(texture.Height / 64) * 64 != texture.Height)
            {
                if (!MenuWidgets.Contains(message))
                    MenuWidgets.Add(message);
                return;
            }

            int width = texture.Width / 64;
            int height = texture.Height / 64;
            int runs = width * height * 64 * 64;
            bool flipX, flipY;
            var data = new Color[texture.Width * texture.Height];
            texture.GetData(data);

            HeightmapEditor.Instance.GenerateNew(width, height);

            for (int cx = 0; cx < width; cx++)
            {
                for (int cy = 0; cy < height; cy++)
                {
                    for (int x = 0; x < 64; x++)
                    {
                        for (int y = 0; y < 64; y++)
                        {
                            progress.Progress = (((cx * 64 + x) + (cy * 64 + y) * texture.Width + 1) / (float)runs) * 100;

                            HeightmapEditor.Instance.VertexChunks[cx + cy * width][x + y * 65].Position.Y = (float)(data[(cx * 64 + x) + (cy * 64 + y) * (width * 64)].R - 128) / 5;

                            flipX = false;
                            flipY = false;

                            if (cx > 0 && x == 0)
                            {
                                HeightmapEditor.Instance.VertexChunks[(cx - 1) + cy * width][64 + y * 65].Position.Y = (float)(data[(cx * 64 + x) + (cy * 64 + y) * (width * 64)].R - 128) / 5;
                                flipX = true;
                            }
                            if (cy > 0 && y == 0)
                            {
                                HeightmapEditor.Instance.VertexChunks[cx + (cy - 1) * width][x + 64 * 65].Position.Y = (float)(data[(cx * 64 + x) + (cy * 64 + y) * (width * 64)].R - 128) / 5;
                                flipY = true;
                            }
                            if (flipX && flipY)
                            {
                                HeightmapEditor.Instance.VertexChunks[(cx - 1) + (cy - 1) * width][65 * 65 - 1].Position.Y = (float)(data[(cx * 64 + x) + (cy * 64 + y) * (width * 64)].R - 128) / 5;
                            }
                        }
                    }
                }
            }
        }
    }
}
